﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands
{
    public class SheepArchivesDetailHandler : IRequestHandler<SheepArchivesDetailCommand, Result<SheepArchivesDetailDto>>
    {
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<SheepFarm> _sheepFarmRepository;
        public SheepArchivesDetailHandler(IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<SheepFarm> sheepFarmRepository)
        {
            _sheepArchivesRepository = sheepArchivesRepository;
            _sheepFarmRepository = sheepFarmRepository;
        }
        /// <summary>
        /// 档案详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<SheepArchivesDetailDto>> Handle(SheepArchivesDetailCommand request, CancellationToken cancellationToken)
        {
            var result  = new Result<SheepArchivesDetailDto>() { Code=(int)CommonErrorCode.Search};
            try
            {
                var sheepArchives = await (from a in await _sheepArchivesRepository.GetAll()
                                           join b in await _sheepFarmRepository.GetAll() on a.FarmId equals b.FarmId
                                           select new SheepArchivesDetailDto
                                           {
                                               RecordId = a.RecordId,
                                               RecordAuricle = a.RecordAuricle,
                                               RecordCorpuscle = a.RecordCorpuscle,
                                               RecordMother = a.RecordMother,
                                               RecordFather = a.RecordFather,
                                               RecordBreed = a.RecordBreed,
                                               RecordBorn = a.RecordBorn,
                                               RecordDate = a.RecordDate,
                                               RecordSite = a.RecordSite,
                                               RecordWeight = a.RecordWeight,
                                               RecordRemark = a.RecordRemark,
                                               AdmissionDate = a.AdmissionDate,
                                               Sex = a.Sex,
                                               Gene = a.Gene,
                                               RecordType = a.RecordType,
                                               FarmId = b.FarmId,
                                               FarmFullName = b.FarmFullName,
                                               FieldId = a.FieldId,
                                               CottageId = a.CottageId,
                                               SheepImage = a.SheepImage,
                                               Stage = a.Stage,
                                               ProductionGrade = a.ProductionGrade,
                                               SalesGrade = a.SalesGrade,
                                               ReproductiveState = a.ReproductiveState,
                                               ReproductiveStateDays = a.ReproductiveStateDays,
                                               PresenceStatus = a.PresenceStatus,
                                               IsPregnancyTest = a.IsPregnancyTest,
                                               WeanWeight = a.WeanWeight,
                                               WeanDate = a.WeanDate,
                                               CreatedOwnerId = a.CreatedOwnerId,
                                               CreatedTime = a.CreatedTime,
                                               UpdatedOwnerId = a.UpdatedOwnerId,
                                               UpdatedTime = a.UpdatedTime,

                                           }).FirstOrDefaultAsync();
                if (sheepArchives != null)
                {
                    result.Data = sheepArchives;
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
